package com.basis.utils;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 为字符串提供常数和静态方法。
 */
public final class StringUtil {
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString().replace("-", "");
		return uuid;
	}
	/**
	 * 判断该string在去掉空格,回车换行，tab字符后是否为空，
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNumber(String value) {
		if (value == null)
			return false;
		return Pattern.matches("^\\d+$", value);
	}

	public static String getChinese(String str) {
		if (str == null)
			return null;
		String regex = "[//u4E00-//u9FFF]+";//[//u4E00-//u9FFF]为汉字   
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			sb.append(matcher.group());
		}
		return sb.toString();
	}

	public static boolean isEmail(String email) {
		if (email == null)
			return false;
		String regex = "[0-9a-zA-Z]+_?[0-9a-zA-Z]+@[0-9a-zA-Z]+\\.[0-9a-zA-Z]+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}
	/**
	 * 1、转换字符串为null的返回不含任何字符的字符串 <br/> 2、输入字符串为"null",则返回不含任何字符的字符串
	 * @param str - 待处理的字符串
	 * @return 处理过null、"null"的字符串
	 */
	public static String killNull(String str) {
		if (str == null || "null".equals(str.toLowerCase()))
			return "";
		return str;
	}

	/**
	 * 返回输入字符串后去除掉左边空字符串
	 * @param str - 待处理的字符串
	 * @return 去除了左边空字符串的字符串
	 */
	public static String trim(String str) {
		return killNull(str).trim();
	}

	/**
	 * 把输入字符串转换为ASCII码表示的字符串 如：输入字符串为"1234",返回"49505152"
	 */
	public static String stringToASCII(String transParam) {
		if (transParam == null || transParam.length() == 0)
			return null;
		char transChars[] = transParam.toCharArray();
		// String ascii = "";
		int charASCII = -1;
		StringBuilder sb = new StringBuilder("");
		for (int i = 0; i < transChars.length; i++) {
			charASCII = transChars[i];
			if (charASCII == 73 || charASCII == 79)
				charASCII++;
			sb.append(charASCII);
		}
		return sb.toString();
	}
	/**
	 * 判断输入字符串是否为空
	 */
	public static boolean isEmpty(Object input) {
		return input == null || "".equals(input);
	}
	/**
	 * 判断输入字符串是否为空
	 */
	public static boolean isNotEmpty(Object input) {
		return input != null && !"".equals(input);
	}
	
	public static Object clobToString(Object clob) {
		if (clob == null || !(clob instanceof Clob)) {
			return clob;
		}
		StringBuffer sb = new StringBuffer();
		Reader is = null;
		try {
			is = ((Clob)clob).getCharacterStream();
			BufferedReader br = new BufferedReader(is);
			String str = null;
			while ((str = br.readLine()) != null) { // 如果没有到达流的末尾，则继续读取下一行
				if (sb.length() > 0) {
					sb.append("\r\n");
				}
				sb.append(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}
